package com.xray.netdisk;

import com.xray.netdisk.pojo.FileItem;
import com.xray.netdisk.utils.NetworkUtils;

import java.util.List;
import java.util.Scanner;

import static com.xray.netdisk.utils.FileUtils.*;

/**
 * 命令行客户端
 */
public class Client {

    private Scanner scanner = new Scanner(System.in);

    public void printFileList(){
        List<FileItem> fileList = NetworkUtils.getFileList();
        System.out.println("网盘文件：");
        fileList.forEach(item ->
                System.out.printf("序号：%d\t文件名：%s\t\t\t大小：%d\n",item.getId(),item.getFilename(),item.getLength()));
    }

    public void uploadFile(){
        System.out.println("请输入文件完整地址：");
        String path = scanner.next();
        NetworkUtils.uploadFile(path);
    }

    public void downloadFile(){
        System.out.println("请输入文件名称：");
        String name = scanner.next();
        NetworkUtils.downloadFile(name);
    }

    public void deleteFile(){
        System.out.println("请输入文件名称：");
        String name = scanner.next();
        NetworkUtils.deleteFile(name);
    }

    public void start(){
        for(;;){
            System.out.println("请输入你的操作：1、查看文件\t2、上传文件\t3、下载文件\t4、删除文件\t其他任意数字退出");
            int type = scanner.nextInt();
            switch(type){
                case TYPE_LIST:
                    printFileList();
                    break;
                case TYPE_UPLOAD:
                    uploadFile();
                    break;
                case TYPE_DOWNLOAD:
                    downloadFile();
                    break;
                case TYPE_DELETE:
                    deleteFile();
                    break;
                default:
                    return;
            }
        }
    }

    public static void main(String[] args) {
        new Client().start();
    }
}
